/**
 * @file
 * @brief 测试程序：EEPROM
 * @author
 * + 隐星魂 (Roy Sun) <xwos@xwos.tech>
 * @copyright
 * + Copyright © 2015 xwos.tech, All Rights Reserved.
 * > Licensed under the Apache License, Version 2.0 (the "License");
 * > you may not use this file except in compliance with the License.
 * > You may obtain a copy of the License at
 * >
 * >         http://www.apache.org/licenses/LICENSE-2.0
 * >
 * > Unless required by applicable law or agreed to in writing, software
 * > distributed under the License is distributed on an "AS IS" BASIS,
 * > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * > See the License for the specific language governing permissions and
 * > limitations under the License.
 */

#include "board/std.h"
#include <xwos/lib/xwlog.h>
#include <xwcd/peripheral/i2c/eeprom/driver.h>
#include "RTD/xwds/device.h"

void tst_eeprom_read(void)
{
        xwu8_t rdbuf[64];
        xwsz_t rdsz;
        xwer_t rc;

        rdsz = sizeof(rdbuf);
        rc = xwds_eeprom_pgread(&rtdxwds_eeprom_256k,
                                rdbuf, &rdsz, 0, xwtm_ft(xwtm_s(2)));
        if (rc < 0) {
                xwlogf(E, "T.EEPROM", "Failed to page read ... %d\r\n", rc);
        }
        rdbuf[rdsz] = '\0';
        xwlogf(I, "T.EEPROM", "page read: %s\r\n", rdbuf);
}

void tst_eeprom_write(void)
{
        xwu8_t wrbuf[32] = "EEPROM.TEST.WRITE.STRING";
        xwu8_t rdbuf[32];
        xwsz_t tstsz;
        xwer_t rc;

        tstsz = sizeof(wrbuf);
        rc = xwds_eeprom_pgwrite(&rtdxwds_eeprom_256k,
                                 wrbuf, &tstsz, 0, xwtm_ft(xwtm_s(2)));
        if (rc < 0) {
                xwlogf(E, "T.EEPROM", "Failed to page write ... %d\r\n", rc);
        }
        tstsz = sizeof(rdbuf);
        rc = xwds_eeprom_pgread(&rtdxwds_eeprom_256k,
                                rdbuf, &tstsz, 0, xwtm_ft(xwtm_s(2)));
        if (rc < 0) {
                xwlogf(E, "T.EEPROM", "Failed to page read ... %d\r\n", rc);
        }
        rdbuf[tstsz] = '\0';
        xwlogf(I, "T.EEPROM", "page read after writing: %s\r\n", rdbuf);
}
